#define  _KEY_GLOBAL_
#include "config.h"

typedef struct _tKEY_ATTRIBUTION_V2
{
	U8 KeyIDShort;
	U8 KeyIDLong;
	U8 DownAttribution;
	U8 ShortAttribution;
	U8 LongAttribution;
	U8 ContinueAttribution;
	U8 UpAttribution;
	U32 LongStartCount;
	U32 LongSpaceCount;
}tKEY_ATTRIBUTION_V2;

/* 
*	iCommand key
*/
const tKEY_ATTRIBUTION_V2 keyAtt[] =
{
	{KB_CENTER,KB_CENTER,1,0,1,0,1,1200,500},
	{KB_NAV,KB_NAV,1,0,1,0,1,1200,500},
	{KB_BACK,KB_BACK,1,0,1,0,1,1200,100},
	
	{KB_WEST,KB_REWIND,1,0,1,1,1,1200,200},
	{KB_EAST,KB_FF,1,0,1,1,1,1200,200},
	{KB_SOUTH,KB_SOUTH,1,0,1,1,1,1200,100},
	{KB_NORTH,KB_NORTH,1,0,1,1,1,1200,100},
	
	{KB_NORTHWEST,KB_NORTHWEST,1,0,1,1,1,1200,100},
	{KB_SOUTHWEST,KB_SOUTHWEST,1,0,1,1,1,1200,100},
	{KB_NORTHEAST,KB_NORTHEAST,1,0,1,1,1,1200,100},
	{KB_SOUTHEAST,KB_SOUTHEAST,1,0,1,1,1,1200,100},
    
    {KB_DEL,KB_DEL,1,0,1,0,1,1200,500},
    
	{KB_MEDIA,KB_MEDIA,1,0,1,0,1,1200,500},
	{KB_TEL,KB_TEL,1,0,1,0,1,1200,500},
	{KB_RADIO,KB_RADIO,1,0,1,0,1,1200,500},
    {KB_CAR,KB_CAR,1,0,1,0,1,1200,500},
    {KB_RIGHT_STAR,KB_RIGHT_STAR,1,0,1,0,1,1200,500},
	{KB_NP,KB_NP,1,0,0,0,0,1200,0},
};

/*
*	steering wheel keys
*/
const tKEY_ATTRIBUTION_V2 swckeyAtt[] =
{
	{KB_SWC_HOME,KB_SWC_HOME,1,0,1,0,1,1200,500},
	{KB_SWC_RETURN,KB_SWC_RETURN,1,0,1,0,1,1200,100},
	{KB_SWC_VOL_DEC,KB_SWC_VOL_DEC,1,0,1,0,1,1200,100},
	{KB_SWC_VOL_INC,KB_SWC_VOL_INC,1,0,1,0,1,1200,100},
	{KB_NP,KB_NP,1,0,0,0,0,1200,0},
};

const tKEY_ATTRIBUTION_V2 *getKeyAtt(U8 keyID)
{
	const tKEY_ATTRIBUTION_V2 *pKeyAtt = &keyAtt[0];

	while (KB_NP != pKeyAtt->KeyIDShort)
	{
		if (keyID == pKeyAtt->KeyIDShort)
		{
			break;
		}
		pKeyAtt++;
	}
	return pKeyAtt;
}

const tKEY_ATTRIBUTION_V2 *getSwcKeyAtt(U8 keyID)
{
	const tKEY_ATTRIBUTION_V2 *pKeyAtt = &swckeyAtt[0];

	while (KB_NP != pKeyAtt->KeyIDShort)
	{
		if (keyID == pKeyAtt->KeyIDShort)
		{
			break;
		}
		pKeyAtt++;
	}
	return pKeyAtt;
}


void swckeyPressProc(BOOL bDown)
{
	const tKEY_ATTRIBUTION_V2 *pKeyAtt = getSwcKeyAtt(swckeyInfo.keyID);

	if (KB_NP == pKeyAtt->KeyIDShort && KB_NP == pKeyAtt->KeyIDLong)
	{
		swckeyInfo.keyContinueProc = FALSE;
		swckeyInfo.keyDownTime = 0;
		return;
	}
	
	if (!bDown)
	{
//		if (!keyInfo.keyContinueProc)
//		{
//		}
//		else
		{
			if (pKeyAtt->UpAttribution)
			{
				if (GetTickCount() - swckeyInfo.firstDownTime >= pKeyAtt->LongStartCount )  
				{
					ipcStartEvent(EVENT_GLOBAL_KEY_LONG_REALSE_EXEC_PROC,pKeyAtt->KeyIDShort,NULL,NULL); // long up
				}
				else
				{
					ipcStartEvent(EVENT_GLOBAL_KEY_SHORT_REALSE_EXEC_PROC,pKeyAtt->KeyIDShort,NULL,NULL); // short up
				}	
				
			}
			else if (pKeyAtt->ShortAttribution)
			{
				ipcStartEvent(EVENT_GLOBAL_KEY_SHORT_REALSE_EXEC_PROC,pKeyAtt->KeyIDShort,NULL,NULL);
			}
		}

		swckeyInfo.keyID = KB_NP;
	}
	else
	{
		if (0 == swckeyInfo.keyDownTime)
		{
			swckeyInfo.keyContinueProc = TRUE;
			swckeyInfo.keyDownTime = GetTickCount();
			swckeyInfo.firstDownTime = swckeyInfo.keyDownTime;
			if (pKeyAtt->DownAttribution)
			{
				ipcStartEvent(EVENT_GLOBAL_KEY_SHORT_PRESS_EXEC_PROC,pKeyAtt->KeyIDShort,NULL,NULL);
			}
			else if (pKeyAtt->ShortAttribution && !pKeyAtt->LongAttribution)
			{
				ipcStartEvent(EVENT_GLOBAL_KEY_SHORT_PRESS_EXEC_PROC,pKeyAtt->KeyIDShort,NULL,NULL);
			}
		}
		else
		{
			if (!swckeyInfo.keyContinueProc)
			{
				if (pKeyAtt->ContinueAttribution)
				{
					if (GetTickCount() - swckeyInfo.keyDownTime >= pKeyAtt->LongSpaceCount)
					{
						swckeyInfo.keyDownTime = GetTickCount();

						ipcStartEvent(EVENT_GLOBAL_KEY_SHORT_PRESS_EXEC_PROC,pKeyAtt->KeyIDLong,NULL,NULL);
					}
				}
			}
			else
			{
				if (pKeyAtt->LongAttribution)
				{
					if (GetTickCount() - swckeyInfo.keyDownTime >= pKeyAtt->LongStartCount)
					{
						swckeyInfo.keyContinueProc = FALSE;
						swckeyInfo.keyDownTime = GetTickCount();
						ipcStartEvent(EVENT_GLOBAL_KEY_LONG_PRESS_EXEC_PROC,pKeyAtt->KeyIDLong,NULL,NULL);
					}
				}
			}
		}
	}
	
}

void keyPressProc(BOOL bDown)
{
	const tKEY_ATTRIBUTION_V2 *pKeyAtt = getKeyAtt(keyInfo.keyID);

	if (KB_NP == pKeyAtt->KeyIDShort && KB_NP == pKeyAtt->KeyIDLong)
	{
		keyInfo.keyContinueProc = FALSE;
		keyInfo.keyDownTime = 0;
		return;
	}
	
	if (!bDown)
	{
//		if (!keyInfo.keyContinueProc)
//		{
//		}
//		else
		{
			if (pKeyAtt->UpAttribution)
			{
				if (GetTickCount() - keyInfo.firstDownTime >= pKeyAtt->LongStartCount )  
				{
					ipcStartEvent(EVENT_GLOBAL_KEY_LONG_REALSE_EXEC_PROC,pKeyAtt->KeyIDShort,NULL,NULL); // long up
				}
				else
				{
					ipcStartEvent(EVENT_GLOBAL_KEY_SHORT_REALSE_EXEC_PROC,pKeyAtt->KeyIDShort,NULL,NULL); // short up
				}	
				
			}
			else if (pKeyAtt->ShortAttribution)
			{
				ipcStartEvent(EVENT_GLOBAL_KEY_SHORT_REALSE_EXEC_PROC,pKeyAtt->KeyIDShort,NULL,NULL);
			}
		}

		keyInfo.keyID = KB_NP;
	}
	else
	{
		if (0 == keyInfo.keyDownTime)
		{
			keyInfo.keyContinueProc = TRUE;
			keyInfo.keyDownTime = GetTickCount();
			keyInfo.firstDownTime = keyInfo.keyDownTime;
			if (pKeyAtt->DownAttribution)
			{
				ipcStartEvent(EVENT_GLOBAL_KEY_SHORT_PRESS_EXEC_PROC,pKeyAtt->KeyIDShort,NULL,NULL);
			}
			else if (pKeyAtt->ShortAttribution && !pKeyAtt->LongAttribution)
			{
				ipcStartEvent(EVENT_GLOBAL_KEY_SHORT_PRESS_EXEC_PROC,pKeyAtt->KeyIDShort,NULL,NULL);
			}
		}
		else
		{
			if (!keyInfo.keyContinueProc)
			{
				if (pKeyAtt->ContinueAttribution)
				{
					if (GetTickCount() - keyInfo.keyDownTime >= pKeyAtt->LongSpaceCount)
					{
						keyInfo.keyDownTime = GetTickCount();

						ipcStartEvent(EVENT_GLOBAL_KEY_SHORT_PRESS_EXEC_PROC,pKeyAtt->KeyIDLong,NULL,NULL);
					}
				}
			}
			else
			{
				if (pKeyAtt->LongAttribution)
				{
					if (GetTickCount() - keyInfo.keyDownTime >= pKeyAtt->LongStartCount)
					{
						keyInfo.keyContinueProc = FALSE;
						keyInfo.keyDownTime = GetTickCount();
						ipcStartEvent(EVENT_GLOBAL_KEY_LONG_PRESS_EXEC_PROC,pKeyAtt->KeyIDLong,NULL,NULL);
					}
				}
			}
		}
	}
}

void keyFreeIdleProc(void)
{
	//keyPressProc(TRUE);
	//swckeyPressProc(TRUE);
}

void ipcEventProcKey(ULONG enumWhatEvent,ULONG lPara,BYTE *p,ULONG length)
{
	BOOL bNeedClear = TRUE;
	switch (enumWhatEvent)
	{
		case EVENT_GLOBAL_KEY_PRESS:
			keyInfo.keyID = lPara;
			keyPressProc(TRUE);
			break;

		case EVENT_GLOBAL_KEY_RELEASE:
			keyInfo.keyID = lPara;
			keyPressProc(FALSE);
			break;
		case EVENT_GLOBAL_SWCKEY_PRESS:
			swckeyInfo.keyID = lPara;
			swckeyPressProc(TRUE);
			break;
		case EVENT_GLOBAL_SWCKEY_RELEASE:
			swckeyInfo.keyID = lPara;
			swckeyPressProc(FALSE);
			break;
		default:
			bNeedClear = FALSE;
			break;
	}

	if (bNeedClear)
	{
		ipcClearEvent(enumWhatEvent);
	}
}

void keyInit(void)
{
	memset(&keyInfo,0,sizeof(KEY_INFO));
}
